package com.riche.aicodehelper.ai;

import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: AiCodeHelperServiceFactory
 * @Description: ai接口的工厂类
 * @Author: Gaoruiqi
 * @Date: 2025-09-25 19:46
 * @Version: 1.0
 **/
@Configuration
public class AiCodeHelperServiceFactory {

    @Resource
    private ChatModel myQwenChatModel;

    @Resource
    private McpToolProvider mcpToolProvider;

    @Resource
    private StreamingChatModel qwenStreamingChatModel;

//    @Resource
//    private ContentRetriever contentRetriever;

    /**
     * 运用反射和动态代理来创建ai服务接口
     *
     * @return
     */
    @Bean
    public AiCodeHelperService aiCodeHelperService() {
        // todo 创建一个消息窗口，保存最近10条消息
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        // 创建ai服务接口, 目前存储在内存中，重启项目就会丢失
        return AiServices.builder(AiCodeHelperService.class)
                .chatMemory(chatMemory)
                .chatModel(myQwenChatModel)
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))  // 创建一个消息窗口，保存最近10条消息
//                .contentRetriever(contentRetriever)  todo Rag检索增强生成
//                .tools(mcpToolProvider)  todo 工具调用
                .toolProvider(mcpToolProvider)
                .streamingChatModel(qwenStreamingChatModel)
                .build();
    }
}
